Skip to content

Comments

Fix issue #280: Add support for persistent callbacks#282

Merged
happydog-intj merged 1 commit intomasterfrom
openhands-workspace-4mkmbnes
Jun 13, 2025
Merged

Fix issue #280: Add support for persistent callbacks#282
happydog-intj merged 1 commit intomasterfrom
openhands-workspace-4mkmbnes

Conversation

@happydog-intj
Copy link
Owner

Summary

This PR fixes issue #280 by adding support for persistent callbacks that can be reused multiple times without being deleted after first use.

Problem

The original issue reported that when caching a callback function from a JavaScript handler and trying to reuse it multiple times from the Android side, it doesn't work correctly after the first use. This was because callbacks were being deleted from the responseCallbacks object after first use, preventing reuse.

Solution

JavaScript Side Changes

  • Added persistentCallbacks object to track callbacks that should not be deleted
  • Added callHandlerPersistent() method for creating persistent callbacks
  • Added registerPersistentCallback() and removePersistentCallback() methods for manual management
  • Modified _doSend() and _dispatchMessageFromNative() to check if a callback is persistent before deleting it

Java Side Changes

  • Added mPersistentCallbacks map to track persistent callbacks
  • Added callHandlerPersistent() method for creating persistent callbacks
  • Added doSendPersistent() method for internal handling
  • Updated BaseJavascriptInterface.response() to not delete persistent callbacks
  • Added getter for persistent callbacks map

Additional Improvements

  • Added comprehensive unit tests for persistent callback functionality
  • Added demo HTML page showing usage examples
  • Updated README with documentation for the new feature
  • Added test dependencies to build.gradle

Usage Examples

Java Side

// Use persistent callback that won't be deleted after first use
webView.callHandlerPersistent("functionInJs", data, new OnBridgeCallback() {
    @Override
    public void onCallBack(String data) {
        // This callback can be called multiple times
        Log.d(TAG, "Persistent callback called: " + data);
    }
});

JavaScript Side

// Use persistent callback
WebViewJavascriptBridge.callHandlerPersistent("javaHandler", data, function(response) {
    // This callback can be reused multiple times
    console.log("Persistent callback response: " + response);
});

Testing

  • Added unit tests to verify persistent callbacks are not deleted after use
  • Added tests to verify normal callbacks still work as before
  • Added demo HTML page for manual testing
  • All existing functionality remains unchanged

Backward Compatibility

This change is fully backward compatible. All existing code will continue to work exactly as before. The new persistent callback functionality is opt-in through new methods.

Fixes #280

@uknownothingsnow can click here to continue refining the PR

- Add persistent callback functionality to prevent callbacks from being deleted after first use
- Implement callHandlerPersistent() method for Java side
- Implement callHandlerPersistent() method for JavaScript side
- Add registerPersistentCallback() and removePersistentCallback() methods
- Update BaseJavascriptInterface to support persistent callbacks
- Add comprehensive tests for persistent callback functionality
- Add demo HTML page showing persistent callback usage
- Update README with documentation for new persistent callback feature

This allows callbacks to be cached and reused multiple times, fixing the issue where Android side couldn't reuse cached callbacks from JavaScript handlers.
@happydog-intj happydog-intj marked this pull request as ready for review June 13, 2025 06:15
@happydog-intj happydog-intj merged commit 78291ba into master Jun 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

嗨,是否支持,持续回调

2 participants